<?php

define("TOKEN", "guohengxi");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{
    public function valid()
    {
        if(isset($_GET["echostr"])){
            $echoStr = $_GET["echostr"]; //随机字符串
            if ($this->checkSignature()) {
                echo $echoStr;
                exit;
            }
        }else{
            echo '测试微信调用.<br/>';
            $postStr = file_get_contents("php://input");
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fu = (string)$postObj->FromUserName;
            $tu = (string)$postObj->ToUserName;
            $MsgType = (string)$postObj->MsgType;
            $content = (string)$postObj->Content;
           echo 'FromUserName:'.$fu.'<br/>'.
                'ToUserName:'.$tu.'<br/>'.
                'MsgType:'.$MsgType.'<br/>'.
                'Content:'.$content.'<br/>';
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"]; //微信加密签名
        $timestamp = $_GET["timestamp"]; //时间戳
        $nonce = $_GET["nonce"]; //随机数

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr); //进行字典序排序

//sha1加密后与签名对比
        if (sha1(implode($tmpArr)) == $signature) {
            return true;
        } else {
            return false;
        }
    }
}
?>